package com.tz.cartelo.activity;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tz.cartelo.R;
import com.tz.cartelo.constant.Api;
import com.tz.cartelo.constant.Key;
import com.tz.cartelo.model.DataParams;
import com.tz.cartelo.model.Login;
import com.tz.cartelo.result.ResultGetCode;
import com.tz.cartelo.result.ResultLogin;
import com.tz.common.util.LogUtil;
import com.tz.common.util.PreferencesUtils;
import com.tz.common.util.VolleyDataUtils;

/**
 * Created by Administrator on 2017/4/16.
 */

public class ChangeLoginPasswordActivity extends DataLoadActivity implements View.OnClickListener {
    private static final String TAG = "ChangeLoginPasswordActivity";
    private EditText mChangePasswordNewPassword, mChangePasswordNewPasswordAgain;
    private ImageView mChangePasswordNewPasswordClean, mChangePasswordNewPasswordAgainClean, mChangePasswordShowPassword;
    private LinearLayout mChangePasswordLayout, mChangePasswordSucceedLayout;
    private Api mApi;
    private boolean mIsShowPassword;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_change_login_password;
    }

    @Override
    protected void init() {
        mChangePasswordLayout = (LinearLayout) findViewById(R.id.change_password_layout);
        mChangePasswordSucceedLayout = (LinearLayout) findViewById(R.id.change_password_succeed_layout);
        mChangePasswordNewPassword = (EditText) findViewById(R.id.change_password_new_password);
        mChangePasswordNewPasswordAgain = (EditText) findViewById(R.id.change_password_new_password_again);
        mChangePasswordNewPasswordClean = (ImageView) findViewById(R.id.change_password_new_password_clean);
        mChangePasswordNewPasswordAgainClean = (ImageView) findViewById(R.id.change_password_new_password_again_clean);
        mChangePasswordShowPassword = (ImageView) findViewById(R.id.change_password_show_password_img);
        mChangePasswordNewPasswordClean.setOnClickListener(this);
        mChangePasswordNewPasswordAgainClean.setOnClickListener(this);
        findViewById(R.id.change_password_show_password).setOnClickListener(this);
        findViewById(R.id.change_password_btn).setOnClickListener(this);
        findViewById(R.id.change_password_succeed).setOnClickListener(this);

        mChangePasswordNewPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b)
                    mChangePasswordNewPasswordClean.setVisibility(View.VISIBLE);
                else
                    mChangePasswordNewPasswordClean.setVisibility(View.GONE);
            }
        });
        mChangePasswordNewPasswordAgain.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b)
                    mChangePasswordNewPasswordAgainClean.setVisibility(View.VISIBLE);
                else
                    mChangePasswordNewPasswordAgainClean.setVisibility(View.GONE);
            }
        });
    }

    @Override
    protected void initTitle() {
        ImageView titleBack = (ImageView) findViewById(R.id.title_back);
        titleBack.setVisibility(View.VISIBLE);
        titleBack.setOnClickListener(this);

        TextView titleText = (TextView) findViewById(R.id.title_text);
        titleText.setText(R.string.change_password_title);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.change_password_new_password_clean:
                mChangePasswordNewPassword.setText("");
                break;
            case R.id.change_password_new_password_again_clean:
                mChangePasswordNewPasswordAgain.setText("");
                break;
            case R.id.change_password_show_password:
                if (mIsShowPassword) {
                    mChangePasswordNewPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mChangePasswordNewPasswordAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mIsShowPassword = false;
                    mChangePasswordShowPassword.setImageResource(R.mipmap.ic_password_close);
                } else {
                    mChangePasswordNewPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mChangePasswordNewPasswordAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mIsShowPassword = true;
                    mChangePasswordShowPassword.setImageResource(R.mipmap.ic_password_show);
                }
                break;
            case R.id.change_password_btn:
                if (mChangePasswordNewPassword.getText().toString().trim().isEmpty()) {
                    showToast(R.string.change_password_empty);
                } else if (mChangePasswordNewPasswordAgain.getText().toString().trim().isEmpty()) {
                    showToast(R.string.change_password_new_password_again_hint);
                } else if (mChangePasswordNewPassword.getText().toString().trim().length() > 12) {
                    showToast(R.string.change_password_wrong);
                } else if (mChangePasswordNewPasswordAgain.getText().toString().trim().length() > 12) {
                    showToast(R.string.change_password_wrong);
                } else if (!mChangePasswordNewPassword.getText().toString().trim().equals(mChangePasswordNewPasswordAgain.getText().toString().trim())) {
                    showToast(R.string.change_password_dif);
                } else {
                    mApi = Api.MODIFY_PASSWORD;
                    loadData(mApi, true);
                }
                break;
            case R.id.change_password_succeed:
                switchActivityAndFinish(MainActivity.class, null);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initParams(DataParams params) {
        switch (mApi) {
            case MODIFY_PASSWORD:
                VolleyDataUtils.mHeaders.put("Cookie", "JSESSIONID=" + PreferencesUtils.getString(Key.SESSION_ID, ""));
                params.addParam("phone", getIntent().getStringExtra(Key.SAFE_TEST_PHONE));
                params.addParam("password", mChangePasswordNewPassword.getText().toString().trim());
                params.addParam("uniqueIdentifier", getIntent().getStringExtra(Key.MSE_CODE));
                break;
            default:
                break;
        }

    }

    @Override
    protected void disposeResult(Api api, String response) {
        LogUtil.d(TAG, response);
        ResultGetCode resultGetCode;
        switch (api) {
            case MODIFY_PASSWORD:
                ResultLogin mResultLogin = fromJson(response, ResultLogin.class);
                if (!mResultLogin.isSuccess()) {
                    showToast(mResultLogin.getMessage());
                    return;
                }
//                showToast(R.string.login_succeed);
                saveLoginData(mResultLogin.getLogin());
                break;
            default:
                break;
        }
    }

    private void saveLoginData(Login loginData) {
        PreferencesUtils.putString(Key.PRE_USER_SEX, loginData.getSex());
        PreferencesUtils.putString(Key.PER_USER_PHONE, loginData.getMobilePhone());
        PreferencesUtils.putString(Key.PER_USER_EMAIL, loginData.getEmail());
        PreferencesUtils.putString(Key.PER_USER_NICK, loginData.getNickName());
        PreferencesUtils.putString(Key.PER_USER_COMPANY, loginData.getCompany());
        PreferencesUtils.putString(Key.PER_USER_ID, loginData.getUserId()+"");
        PreferencesUtils.putInt(Key.PER_USER_GRADE, loginData.getGrade());
        PreferencesUtils.putString(Key.PER_USER_HEAD, loginData.getHeadPortrait());
        PreferencesUtils.putString(Key.PER_USER_NAME, loginData.getUserName());
        PreferencesUtils.putInt(Key.PER_USER_TYPE, loginData.getUserType());
        PreferencesUtils.putBoolean(Key.PER_LOGIN, true);

        mChangePasswordLayout.setVisibility(View.GONE);
        mChangePasswordSucceedLayout.setVisibility(View.VISIBLE);
    }
}
